<?php
$a_tablecolumns_namhoa_user_dear = array(
    'name'    => 'user_dear',
    'columns' => array(
        'dearid'       ,
        'saint'        ,
        'userid'       ,
        'dearname'     ,
        'relationship' ,
        'address'      ,
        'mobile'       ,
        'phone'        ,
        'creatorid'    ,
        'updaterid'    ,
        'insertdate'   ,
        'updatedate'   ,
    ),
);

function get_columns_namhoa_user_dear() {
    return array(
        'name'    => 'user_dear',
        'columns' => array(
            'dearid'       ,
            'saint'        ,
            'userid'       ,
            'dearname'     ,
            'relationship' ,
            'address'      ,
            'mobile'       ,
            'phone'        ,
            'creatorid'    ,
            'updaterid'    ,
            'insertdate'   ,
            'updatedate'   ,
        ),
    );
}

$f_dearid       = 0 ;
$f_saint        = '';
$f_userid       = '';
$f_dearname     = '';
$f_relationship = '';
$f_address      = '';
$f_mobile       = '';
$f_phone        = '';
$f_creatorid    = '';
$f_updaterid    = '';
$f_insertdate   = date('Y-m-d H:i:s');
$f_updatedate   = $f_insertdate;

class namhoa_user_dear {
    public function __get($v_property) {
        return '';
    }
} 

$d_user_dear = new namhoa_user_dear();

if (isset($_REQUEST['dearid'       ] )) { $f_dearid       = $_REQUEST['dearid'       ] ; }
if (isset($_REQUEST['saint'        ] )) { $f_saint        = $_REQUEST['saint'        ] ; }
if (isset($_REQUEST['userid'       ] )) { $f_userid       = $_REQUEST['userid'       ] ; }
if (isset($_REQUEST['dearname'     ] )) { $f_dearname     = $_REQUEST['dearname'     ] ; }
if (isset($_REQUEST['relationship' ] )) { $f_relationship = $_REQUEST['relationship' ] ; }
if (isset($_REQUEST['address'      ] )) { $f_address      = $_REQUEST['address'      ] ; }
if (isset($_REQUEST['mobile'       ] )) { $f_mobile       = $_REQUEST['mobile'       ] ; }
if (isset($_REQUEST['phone'        ] )) { $f_phone        = $_REQUEST['phone'        ] ; }

function make_filter_namhoa_user_dear($s_field, $s_keyword) {
    $v_filter = '';

    if ($s_field != '' && $s_keyword != '') {
        switch ($s_field) {
            case 'saint'        :
            case 'userid'       :
            case 'dearname'     :
            case 'relationship' :
            case 'address'      :
            case 'mobile'       :
            case 'phone'        :
            case 'creatorid'    :
            case 'updaterid'    :
            case 'dearid'       :
                $v_filter = "`$s_field` like '%$s_keyword%'";

                break;

            case 'insertdate'   :
            case 'updatedate'   :
                $v_thisday = $v_nextday = date('Y-m-d');

                if (strpos($s_keyword, '~') !== FALSE) {
                    $v_keywords = explode('~', $s_keyword)   ;
                    $v_thisday  = format_date($v_keywords[0]);
                    $v_nextday  = format_date($v_keywords[1]);
                }

                $v_filter = "(`$s_field` >= '$v_thisday 00:00:00' and `$s_field` <= '$v_nextday 23:59:59')";

                break;

        }
    }

    return $v_filter;
}

function make_advanced_filter_namhoa_user_dear($s_field, $s_keyword) {
    $v_filter = '';

    if ($s_field != '' && $s_keyword != '') {
        switch ($s_field) {
            case 'saint'        :
            case 'userid'       :
            case 'dearname'     :
            case 'relationship' :
            case 'address'      :
            case 'mobile'       :
            case 'phone'        :
            case 'creatorid'    :
            case 'updaterid'    :
                $v_filter = "`$s_field` like '%$s_keyword%'";

                break;

            case 'dearid'       :
                if (strpos($s_keyword, '~') !== FALSE) {
                    list($v_from, $v_to) = explode('~', $s_keyword, 2);
                
                    if (is_numeric($v_from)) {
                        $v_filter = $v_filter . "`$s_field` >= $v_from";
                    }
                
                    if (is_numeric($v_to)) {
                        $v_filter = $v_filter . (empty($v_filter) ? '' : ' and ') . "`$s_field` <= $v_to";
                    }
                
                    $v_filter = empty($v_filter) ? '' : "($v_filter)";
                }
                elseif (is_numeric($s_keyword)) {
                    $v_filter = "`$s_field` = $s_keyword";
                }

                break;

            case 'insertdate'   :
            case 'updatedate'   :
                if (strpos($s_keyword, '~') !== FALSE) {
                    list($v_fromday, $v_today) = explode('~', $s_keyword, 2);
                
                    if (validate_date($v_fromday)) {
                        $v_filter = $v_filter . "`$s_field` >= '$v_fromday 00:00:00'";
                    }
                
                    if (validate_date($v_today)) {
                        $v_filter = $v_filter . (empty($v_filter) ? '' : ' and ') . "`$s_field` <= '$v_today 23:59:59'";
                    }
                
                    $v_filter = empty($v_filter) ? '' : "($v_filter)";
                }
                elseif (validate_date($s_keyword)) {
                    $v_filter = "(`$s_field` >= '$s_keyword 00:00:00' and `$s_field` <= '$s_keyword 23:59:59')";
                }

                break;

        }
    }

    return $v_filter;
}

function build_filter_namhoa_user_dear($s_field) {
    echo '<option value="dearid"'       , ($s_field == 'dearid'        ? ' selected="selected"' : ''), '> Mã Số Người Thân </option>';
    echo '<option value="saint"'        , ($s_field == 'saint'         ? ' selected="selected"' : ''), '> Tên Thánh        </option>';
    echo '<option value="userid"'       , ($s_field == 'userid'        ? ' selected="selected"' : ''), '> Mã Số Thiếu Nhi  </option>';
    echo '<option value="dearname"'     , ($s_field == 'dearname'      ? ' selected="selected"' : ''), '> Họ Và Tên        </option>';
    echo '<option value="relationship"' , ($s_field == 'relationship'  ? ' selected="selected"' : ''), '> Mối Quan Hệ      </option>';
    echo '<option value="address"'      , ($s_field == 'address'       ? ' selected="selected"' : ''), '> Địa Chỉ          </option>';
    echo '<option value="mobile"'       , ($s_field == 'mobile'        ? ' selected="selected"' : ''), '> Mobile           </option>';
    echo '<option value="phone"'        , ($s_field == 'phone'         ? ' selected="selected"' : ''), '> Phone            </option>';
    echo '<option value="creatorid"'    , ($s_field == 'creatorid'     ? ' selected="selected"' : ''), '> creatorid        </option>';
    echo '<option value="updaterid"'    , ($s_field == 'updaterid'     ? ' selected="selected"' : ''), '> updaterid        </option>';
    echo '<option value="insertdate"'   , ($s_field == 'insertdate'    ? ' selected="selected"' : ''), '> insertdate       </option>';
    echo '<option value="updatedate"'   , ($s_field == 'updatedate'    ? ' selected="selected"' : ''), '> updatedate       </option>';
}

function build_advanced_filter_namhoa_user_dear($s_field, $a_hide = array()) {
    if (empty($a_hide)) {
        return build_filter_namhoa_user_dear($s_field);
    }

    $a_columns = array(
        'dearid'       => 'Mã Số Người Thân' ,
        'saint'        => 'Tên Thánh'        ,
        'userid'       => 'Mã Số Thiếu Nhi'  ,
        'dearname'     => 'Họ Và Tên'        ,
        'relationship' => 'Mối Quan Hệ'      ,
        'address'      => 'Địa Chỉ'          ,
        'mobile'       => 'Mobile'           ,
        'phone'        => 'Phone'            ,
        'creatorid'    => 'creatorid'        ,
        'updaterid'    => 'updaterid'        ,
        'insertdate'   => 'insertdate'       ,
        'updatedate'   => 'updatedate'       ,
    );

    $a_hide    = array_combine($a_hide, $a_hide);
    $a_columns = array_diff_key($a_columns, $a_hide);

    foreach ($a_columns as $v_columnname => $v_columncomment) {
        echo '<option value="', $v_columnname, '"', ($s_field == $v_columnname ? ' selected="selected"' : ''), '>', $v_columncomment, '</option>';
    }
}
?>